package com.usermanage.Filter;
import com.usermanage.SecurityConfig.SpringSecurityUtils;
import com.usermanage.SecurityConfig.UserDetail;
import com.usermanage.beanParam.PermissionBean;
import com.usermanage.util.ErrMessageUtil;
import com.usermanage.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * Created by 過客 on 2018/3/9
 */
@Slf4j

public class WebFilter implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("doFilter方法进入了");
        UserDetail userDetail = SpringSecurityUtils.getCurrentUser();
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String url = request.getRequestURI();
        log.error("url =" + url);
        log.error("post?get =" + request.getMethod());
        log.error("user =" + SpringSecurityUtils.getCurrentUser());
        //判断是否为ajax请求，默认不是
        if(!StringUtils.isBlank(request.getHeader("x-requested-with")) && request.getHeader("x-requested-with").equals("XMLHttpRequest")){
        }
//        if (userDetail == null){
////            返回json数据
//            response.setContentType("application/json; charset=utf-8");
//            response.setCharacterEncoding("UTF-8");
//            OutputStream out = response.getOutputStream();
////            out.write(StringUtil.ObjectToJson("访问超时，请刷新页面重新登陆").getBytes("UTF-8"));
////            out.flush();
//            //用户登陆信息为空，定位到登陆界面
////            servletRequest.getRequestDispatcher("/userLoginPage").forward(servletRequest,servletResponse);
////            return;
//        }else if (StringUtils.isBlank(userDetail.getGroup_id())&& !"/user/chooseGroupList.json".equals(url)){
//            //用户登陆信息不完整，定位到补全信息页
////            filterChain.doFilter(servletRequest, servletResponse);
////            servletRequest.getRequestDispatcher("/user/chooseGroupList.json").forward(servletRequest,servletResponse);
////            return;
//        }
        /**
         * 取用户访问链接，然后取用户权限，对比看该用户是否有该访问权限
         */
//        List<PermissionBean> list = userDetail.getList();
//        boolean bool = false;
//        for (PermissionBean bean:list) {
//            if (url.equals(bean.getPermission_url())){
//                bool = true;
//                break;
//            }
//        }
//        if(bool){
//            //正常向下走。
//            filterChain.doFilter(servletRequest, servletResponse);
//        }else{
//            //返回错误信息，权限不足。
//            String roleMessageUrl = "/err.page?err="+ ErrMessageUtil.RoleMessage;
//            servletRequest.getRequestDispatcher(roleMessageUrl).forward(servletRequest,servletResponse);
//            return;
//        }
        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {
    }


}
